--
-- Author: mm
-- Date: 2018-06-15 11:31:42
--
-- JewelRender
--

local Node_setVisible = cc.Node.setVisible
local JewelRender = app.mvc.createListViewCell(...)

--data =
function JewelRender.createWithData(data, node)
    local sprite = JewelRender.extend(node or cc.CSLoader:createNode(Res.JewelEquipNodeUi))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function JewelRender.create(node)
    return JewelRender.createWithData(nil, node)
end

--
function JewelRender:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()

    self._ui["Panel/selected_btn"]:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
            self:dispatchCellEvent("click", self._cellModel)
        end
    end)
end

function JewelRender:refresh()
    local selectBtn = self._ui["Panel/selected_btn"]
    selectBtn:setPressedActionEnabled(false)
	if self._data then
		Node_setVisible(self,true)
		
		local btnName = self._ui["Panel/name"]
        local color = Enums.QualityColor[self._data.quality]
	    if color then
		    btnName:setTextColor(color)
	    end
	    btnName:setString(self._data:getName(true))

        local lvPanel = self._ui["Panel/Panel_lv"]
        local itemNode=self._ui["Panel/icon"]
        local levelNode = itemNode:findChild("Panel/level")
        
        Node_setVisible(lvPanel,false)
        Node_setVisible(levelNode,false)
        
        itemNode:findChild("Panel"):setSwallowTouches( false )
        itemNode:findChild("Panel/Panel_num"):setVisible(false)

        local jewelImg = itemNode:findChild("Panel/icon")
        jewelImg:loadTexture(self._data:getIcon())
        local jewelImgBg = itemNode:findChild("Panel/grade")
        jewelImgBg:loadTexture(self._data:getIconBg())
        local selectedImg = itemNode:findChild("Panel/selected")
        selectedImg:setVisible(false)
        local jeweledImg = itemNode:findChild("Panel/equipped")

     
        Node_setVisible(jeweledImg,self._data:isWear())
        local jewelNode=self._ui["Panel/Panel_jewel"]
        Node_setVisible(jewelNode,true)
        self:freshBrightStatus()

        local hasRedPoint = false
        for i=1,4 do
            local jewelTem = self._ui["Panel/Panel_jewel"]:getChildByName("icon_"..i)
            Node_setVisible(jewelTem,false)
            local jewelSlots = self._data:getAllSlots()
            local euid = self._data.uid
            if jewelSlots[i] then
                self:showJewelNode(jewelSlots[i],jewelTem)
            else
                Node_setVisible(jewelTem,false)
            end
            if app:sendMsg("JewelModel", "slotHasRedPoint", euid, i) then
                jewelTem:findChild("Panel/dot"):show()
                hasRedPoint = true
            else
                jewelTem:findChild("Panel/dot"):hide()
            end
        end

        -- itemNode:findChild("Panel/dot"):setVisible(hasRedPoint)
	end
end

function JewelRender:showJewelNode(slot,jewelNode)
    -- dump(slot)
    jewelNode:setVisible( true )
    local bg_1Node=jewelNode:findChild("Panel/bg_1")
    local bg_2Node=jewelNode:findChild("Panel/bg_2")
    bg_2Node:loadTexture( slot:getIcon() )
    local iconNode=jewelNode:findChild("Panel/icon")
    local numNode=jewelNode:findChild("Panel/num")
    local jewel = slot.jewel

    if jewel then
        iconNode:loadTexture(jewel:getSmallIcon())
        bg_1Node:loadTexture(jewel:getSmallIconBg())
--        local jewelLv = jewel:getLevel()
        -- 客户端将宝石等级主动+1
        local jewelLv = jewel:getLevelPlus1()
        numNode:setVisible( jewelLv > 0 )
        numNode:setString( jewelLv )
        Node_setVisible(bg_1Node,true)
        Node_setVisible(iconNode,true)
        -- 有宝石的时候隐藏宝石孔的背景图
        Node_setVisible(bg_2Node,false)
    else
        Node_setVisible(numNode,false)
        Node_setVisible(bg_1Node,false)
        Node_setVisible(iconNode,false)
        Node_setVisible(bg_2Node,true)
    end
end

function JewelRender:freshBrightStatus( isBright)
    if self._cellModel then
        self._ui["Panel/selected_btn"]:setBright(self._cellModel:getIndex() ~= self._cellModel:getFoucsIndex())
    else
        self._ui["Panel/selected_btn"]:setBright(true)
    end
end

function JewelRender:onBtnClick(cb)
    self._onBtnClick = cb
end

function JewelRender:onFoucsIndexChange(foucsIndex)
    self:freshBrightStatus()
end

return JewelRender
